<template>
 <div>
      <el-tree :data="settings" 
      ref="reftree"
      :props="{label:'name'}" 
      default-expand-all
      show-checkbox 
      check-strictly
      node-key="id"/>
      <div style="text-align: right">
        <el-button @click="$emit('close')">取消</el-button>
        <el-button type="primary" @click="hSubmit">确定</el-button>
      </div>
 </div>
</template>

<script>
import { getpermission} from '@/api/permissions';
import { tranListToTreeData } from '@/utils/index'
import{getassign, updateassign} from  '@/api/roles'
export default {
  name: 'Assignsettings',
  props:{
    id:{
      type:String,
      required:true
    }
  },
  data() {
    return {
      settings:[]
    };
  },

  created() {
    this.loadsettings()
    this.loadassign()
  },

  methods: {
    //获取所有权限
  async  loadsettings(){
    const res = await  getpermission()
    console.log('一个',res.data);
    this.settings=tranListToTreeData(res.data)
    },
    //根据id获取权限点
  async  loadassign(){
    console.log('父传子',this.id);
    const res = await  getassign(this.id)
    console.log('权限点详情',res.data);
    this.$refs.reftree.setCheckedKeys(res.data.permIds)
    },
  async  hSubmit(){
    try{
          const ids=this.$refs.reftree.getCheckedKeys()
      console.log('id值',ids);
    const res = await  updateassign({
        id:this.id,
        permIds:ids
      })
      this.$message.success(res.message)
      this.$emit('close')
    }catch(e){
      this.$message.error(e.message)
    }
    }
  },
};
</script>

<style lang="scss" scoped>

</style>